/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.DVDCtr;
import ModelLayer.*; 

public class DvdUI
{
    private DVDCtr dvdCtr;
    
    /**
     * Constructor for objects of class DvdUI
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    
    /**
     * Menu Section
     * 
     * start(), executes the menu.
     * dvdMenu(), is a selection method for the user menu.
     * writeDVDMenu(), is a visual interpretation of the user menu for the dvd section.
     */
    public void start()
    {
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            int choise =   writeDVDMenu();
            if (choise == 1)
            {
                startFindDVD();
            }
            else{
                if(choise == 2)
                {
                    startCreateDVD();
                }
                else
                {
                    if(choise == 3)
                    {
                        startCreateCopy();
                    }
                    else
                    {
                        if(choise == 4)
                        {
                            startEditDVD();
                        } 
                        else
                        {
                            if(choise == 5)
                            {
                                 startDeleteDVD();
                            }
                            else
                            {
                                if(choise == 6)
                                {
                                    startListAllDvds();
                                }
                                else
                                {
                                    exit = true;
                                }
                            }
                        }
                    }
                }
            }   
        }
    }
    
    private int writeDVDMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD specimen");
        System.out.println(" (4) Edit DVD");
        System.out.println(" (5) Delete DVD");
        System.out.println(" (6) Show current DVD list");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choise: ");
            
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * User input section
     * 
     * inputTitle(), inputs the movies title via user interaction.
     * inputDirector(), inputs the movies director via user interaction.
     * inputReleaseDate(), inputs the movies release date via user interaction.
     * inputCopySerialNo(), inputs the copys serialnumber via user interaction.
     * inputAcquisitionDate(), inputs the copy acquisition date via user interaction.
     * inputAcquisitionPrice() inputs the acquisition price via user interaction.
     */
    private String inputTitle()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    private String inputDirector()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type DVD director: " );
        String director = keyboard.nextLine();
        return director;
    }
    
    private String inputReleaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type DVD release date: " );
        String ReleaseDate = keyboard.nextLine();
        return ReleaseDate;
    }
    
    private long inputCopySerialNo()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type the copy serial number: ");
        long no = keyboard.nextLong();
        return no;
    }
    
    private String inputAcquisitionDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type the acquisition date: ");
        String AcquisitionDate = keyboard.nextLine();
        return AcquisitionDate;
    }
    
    private double inputAcquisitionPrice()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print(" Type the acquisition price: ");
        double AcquisitionPrice = keyboard.nextDouble();
        return AcquisitionPrice;
    }
    
    /**
     * User interaction section
     * 
     * startFindDVD(), finds movie trough its title via its perspectiv array.
     * startCreateDVD(), creates movie information and stores it in array.
     * startCreateCopy(), creates copy information of a movie if the title is found in array.
     * startDeleteDVD(), deletes movie if its title is found in array.
     * startListAllDvds(), lists all movies in array.
     * startEditDVD(), edits the movies information after the title is found via there perspectiv arays.
     * editDVDData(), is a visual interpretation of the dvd edit menu.
     */
    private void startFindDVD()
    {     
        String title = inputTitle();
        DVD DVDobj = dvdCtr.findDVD(title);
        
        try
        {
            if (DVDobj != null)
            {
                System.out.println(" Title: " + DVDobj.getTitle());
                System.out.println(" Director: " + DVDobj.getDirector());
                System.out.println(" Release date: " + DVDobj.getReleaseDate());
                
                ArrayList<Copy> CopyList = DVDobj.getCopyList();
                System.out.println("\n -- Copies --");
                for (Copy copy : CopyList)
                {
                    System.out.println(" Copy serial number: " + copy.getAcquisitionDate());
                    System.out.println(" Copy acquisition date: " + copy.getAcquisitionDate());
                    System.out.println(" Copy acquisition price: " + copy.getAcquisitionPrice() + "\n");
                }
                
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            } 
            else
            {
                System.out.println("\n ** No DVD with that title was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
   
    private void startCreateDVD()
    {
        String title = inputTitle();
        String director = inputDirector();
        String release = inputReleaseDate();
        dvdCtr.createDVD(title, director, release);
    }
    
    private void startCreateCopy()
    {
        String title = inputTitle();
        DVD DVDobj = dvdCtr.findDVD(title);
        
        try
        {
            if(DVDobj != null)
            {
                long serialNo = inputCopySerialNo();
                String acquisitionDate = inputAcquisitionDate();
                double acquisitionPrice = inputAcquisitionPrice();
                dvdCtr.createCopy(DVDobj, serialNo, acquisitionDate, acquisitionPrice);
            }
            else
            {
                System.out.println("\n ** No DVD with that title was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private void startDeleteDVD()
    {
        String title = inputTitle();
        
        try
        {
            if(dvdCtr.deleteDVD(title))
            {
                System.out.println("\n ** The DVD was deleted! **");
                Thread.sleep(1500);
            }
            else
            {
                System.out.println("\n ** No DVD with that ID was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private void startListAllDvds()
    {
        ArrayList<DVD> DVDList = dvdCtr.listAllDvds();
        
        for (DVD dvd : DVDList)
        {
            System.out.println("DVD title: " + dvd.getTitle());
            System.out.println("DVD director: " + dvd.getDirector());
            System.out.println("DVD release date: " + dvd.getReleaseDate());
        }
        
        System.out.println("\n Press the enter key to return to menu");
        Scanner sc = new Scanner(System.in);
        while(!sc.nextLine().equals(""));
    }
    
    private void startEditDVD() 
    {
        String title = inputTitle();
        DVD DVDobj = dvdCtr.findDVD(title);
        
        try
        {
            if (DVDobj != null)
            {
                int nr = editDVDData();
                Scanner keyboardData = new Scanner(System.in); 
                System.out.print(" Enter new data: ");
                String newData = keyboardData.nextLine();
                dvdCtr.editDVDData(DVDobj, nr, newData);
            }
            else
            {
                System.out.println("\n ** No DVD with that title was found! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println(" ** An error has occurred! **");
        }
    }
    
    private int editDVDData() 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" -- Edit DVD --");
        System.out.println(" (1) Edit title");
        System.out.println(" (2) Edit director");
        System.out.println(" (3) Edit release date");
        System.out.print("\n Make your choise: ");
        int choise = keyboard.nextInt();
        return choise;
    }
}